package com.ruoyi.system.service.estate.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.estate.AdvertisementMapper;
import com.ruoyi.system.domain.estate.Advertisement;
import com.ruoyi.system.service.estate.IAdvertisementService;

/**
 * 广告管理Service业务层处理
 */
@Service
public class AdvertisementServiceImpl implements IAdvertisementService {
    @Autowired
    private AdvertisementMapper advertisementMapper;

    /**
     * 查询广告列表
     * 
     * @param advertisement 广告信息
     * @return 广告集合
     */
    @Override
    public List<Advertisement> selectAdvertisementList(Advertisement advertisement) {
        return advertisementMapper.selectAdvertisementList(advertisement);
    }

    /**
     * 查询广告详细信息
     * 
     * @param adId 广告ID
     * @return 广告信息
     */
    @Override
    public Advertisement selectAdvertisementById(Long adId) {
        return advertisementMapper.selectAdvertisementById(adId);
    }

    /**
     * 新增广告
     * 
     * @param advertisement 广告信息
     * @return 结果
     */
    @Override
    public int insertAdvertisement(Advertisement advertisement) {
        // 设置默认值
        if (advertisement.getClickCount() == null) {
            advertisement.setClickCount(0);
        }
        if (advertisement.getSortOrder() == null) {
            advertisement.setSortOrder(0);
        }
        if (advertisement.getAdStatus() == null) {
            advertisement.setAdStatus("0"); // 默认未发布
        }
        advertisement.setCreateTime(new Date());
        advertisement.setCreateBy(SecurityUtils.getUsername());
        advertisement.setDelFlag("0");
        advertisement.setStatus("0");
        return advertisementMapper.insertAdvertisement(advertisement);
    }

    /**
     * 修改广告
     * 
     * @param advertisement 广告信息
     * @return 结果
     */
    @Override
    public int updateAdvertisement(Advertisement advertisement) {
        advertisement.setUpdateTime(new Date());
        advertisement.setUpdateBy(SecurityUtils.getUsername());
        return advertisementMapper.updateAdvertisement(advertisement);
    }

    /**
     * 批量删除广告
     * 
     * @param adIds 需要删除的广告ID
     * @return 结果
     */
    @Override
    public int deleteAdvertisementByIds(Long[] adIds) {
        return advertisementMapper.deleteAdvertisementByIds(adIds);
    }

    /**
     * 删除广告信息
     * 
     * @param adId 广告ID
     * @return 结果
     */
    @Override
    public int deleteAdvertisementById(Long adId) {
        return advertisementMapper.deleteAdvertisementById(adId);
    }
    
    /**
     * 更新广告点击次数
     * 
     * @param adId 广告ID
     * @return 结果
     */
    @Override
    public int updateClickCount(Long adId) {
        return advertisementMapper.updateClickCount(adId);
    }
    
    /**
     * 发布广告
     * 
     * @param adId 广告ID
     * @return 结果
     */
    @Override
    public int publishAdvertisement(Long adId) {
        Advertisement advertisement = new Advertisement();
        advertisement.setAdId(adId);
        advertisement.setAdStatus("1"); // 已发布
        advertisement.setUpdateTime(new Date());
        advertisement.setUpdateBy(SecurityUtils.getUsername());
        return advertisementMapper.updateAdvertisement(advertisement);
    }
    
    /**
     * 下架广告
     * 
     * @param adId 广告ID
     * @return 结果
     */
    @Override
    public int unpublishAdvertisement(Long adId) {
        Advertisement advertisement = new Advertisement();
        advertisement.setAdId(adId);
        advertisement.setAdStatus("2"); // 已下架
        advertisement.setUpdateTime(new Date());
        advertisement.setUpdateBy(SecurityUtils.getUsername());
        return advertisementMapper.updateAdvertisement(advertisement);
    }
} 